﻿using System;
using System.IO;
using System.Windows.Forms;

namespace TextFileMerger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSelectFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt";
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] filePaths = openFileDialog.FileNames;
                if (filePaths.Length == 2)
                {
                    try
                    {
                        string executablePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                        string dataDirectory = Path.Combine(executablePath, "Data");
                        if (!Directory.Exists(dataDirectory))
                        {
                            Directory.CreateDirectory(dataDirectory);
                        }
                        string outputFilePath = Path.Combine(dataDirectory, "MergedFile.txt");

                        using (StreamWriter writer = new StreamWriter(outputFilePath))
                        {
                            foreach (string filePath in filePaths)
                            {
                                string fileContent = File.ReadAllText(filePath);
                                writer.WriteLine(fileContent);
                            }
                        }

                        MessageBox.Show("文件合并成功！合并后的文件路径为：" + outputFilePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("合并文件时出现错误：" + ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请选择两个文本文件。");
                }
            }
        }
    }
}